package com.thti.mecool.client.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;

import com.thti.mecool.client.adapter.DraftAdapter;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.model.biz.PlanBizImpl;
import com.thti.mecool.client.model.po.PlanClient;
import com.thti.mecool.client.widget.Toolbar;

public class DraftActivity extends BaseActivity {

	private ListView draftListView;
	public static int modify = 0;
	List<Map<String, String>> list;
	PlanBizImpl planBizImpl;
	private int curItem;
	BaseAdapter draftAdapter;
	ArrayList<String> planidList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.noTitle();
		Cache.add(this);
		setContentView(R.layout.mecool_draft_plan);
		initToolbar();
		draftListView = (ListView) findViewById(R.id.mecool_draft_plan_list);
		planBizImpl = new PlanBizImpl(this);
		init();
		service();
	}
	@Override
	protected void onResume() {
		super.onResume();
		init();
		service();
	}
	private void service() {
		if(draftAdapter!=null){
			draftAdapter.notifyDataSetChanged();
		}else{
		draftAdapter = new DraftAdapter(this, list);
		draftListView.setAdapter(draftAdapter);
		draftListView.setOnItemClickListener(draftListViewListener);
		draftListView.setOnItemLongClickListener(draftListViewLongListener);
		}
	}

	private OnItemClickListener draftListViewListener = new ListView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Intent intent = new Intent(DraftActivity.this,
					DraftPlanTaskActivity.class);
			intent.putExtra("curPlanid", planidList.get(arg2));
			startActivity(intent);
		}
	};
	private OnItemLongClickListener draftListViewLongListener = new ListView.OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {
			curItem = arg2;
			dialog();
			return false;
		}
	};

	public void init() {
		if (list != null ) {
			if(list.size() > 0){
			list.clear();
			list = null;}
		}
		list = new ArrayList<Map<String, String>>();
		planidList = new ArrayList<String>();
		List<PlanClient> planlist = planBizImpl.findAll();
		for (PlanClient plan : planlist) {
			if (plan.getIsType() == 0) {
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("name", plan.getPlanName());
				map.put("time", plan.getPlanTime());
				list.add(map);
				planidList.add(plan.getPlanId());
			}
		}
	}

	private void initToolbar() {
		Toolbar toolbar = new Toolbar(this);
		toolbar.setToolbarCentreText("草稿箱");
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
		Toolbar.hideRightButton();
		Toolbar.left_btn.setOnClickListener(leftbuttonListener);
	}

	private OnClickListener leftbuttonListener = new ImageButton.OnClickListener() {
		@Override
		public void onClick(View v) {
			finish();
		}
	};

	@Override
	public void dialog() {
		AlertDialog.Builder builder = new Builder(this);
		builder.setMessage("确定要删除吗？").setTitle("提示");
		builder.setPositiveButton("确定", dialogOnClickListener)
				.setNegativeButton("取消", dialogOnClickListener);
		builder.create().show();
	}

	DialogInterface.OnClickListener dialogOnClickListener = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			switch (which) {
			case -1:
				list.remove(curItem);
				planBizImpl.delete(planidList.get(curItem), false);
				planidList.remove(curItem);
				List<Map<String, String>> TempList = new ArrayList<Map<String, String>>();
				TempList.addAll(list);
				list.clear();
				for (Map<String, String> m : TempList) {
					list.add(m);
				}
				List<String> TempPlanidList = new ArrayList<String>();
				TempPlanidList.addAll(planidList);
				planidList.clear();
				for (String s : TempPlanidList) {
					planidList.add(s);
				}
				draftAdapter.notifyDataSetChanged();
				dialog.dismiss();
				break;
			default:
				dialog.dismiss();
			}
		}
	};
}
